package com.tim.app.ui.popup;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.library.runtime.event.EventManager;
import com.tim.app.R;
import com.tim.app.RT;
import com.tim.app.constant.AppKey;
import com.tim.app.constant.EventTag;


public class ReportSomeonePop extends PopupWindow implements View.OnClickListener {

    private Context mContext;
    private View rootView;
    private TextView tvReport, tvCancel, tvCancelFollow, tvRemoveBlack, tvAddToBlack;

    private View vAddToBlack, vRemoveBlack;

    public View getRootView() {
        return rootView;
    }

    public ReportSomeonePop(Context context, int friendType) {
        super(context);
        this.mContext = context;
        setWidth(RT.getScreenWidth());
        setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable());

        rootView = LayoutInflater.from(context).inflate(R.layout.pop_report_someone, null);
        setContentView(rootView);

        tvReport = (TextView) rootView.findViewById(R.id.tvReport);
        tvAddToBlack = (TextView) rootView.findViewById(R.id.tvAddToBlack);
        tvRemoveBlack = (TextView) rootView.findViewById(R.id.tvRemoveBlack);
        tvCancelFollow = (TextView) rootView.findViewById(R.id.tvCancelFollow);
        tvCancel = (TextView) rootView.findViewById(R.id.tvCancel);
        vAddToBlack = rootView.findViewById(R.id.vAddToBlack);
        vRemoveBlack = rootView.findViewById(R.id.vRemoveBlack);
        tvReport.setOnClickListener(this);
        tvAddToBlack.setOnClickListener(this);
        tvRemoveBlack.setOnClickListener(this);
        tvCancelFollow.setOnClickListener(this);
        tvCancel.setOnClickListener(this);

        if (AppKey.RELATIONSHIP_TYPE_FRIEND == friendType) {
            tvCancelFollow.setVisibility(View.VISIBLE);
            tvAddToBlack.setVisibility(View.VISIBLE);
            vAddToBlack.setVisibility(View.VISIBLE);
        } else if (AppKey.RELATIONSHIP_TYPE_FOLLOW == friendType) {
            tvCancelFollow.setVisibility(View.VISIBLE);
            tvAddToBlack.setVisibility(View.VISIBLE);
            vAddToBlack.setVisibility(View.VISIBLE);
        } else if (AppKey.RELATIONSHIP_TYPE_NORMAL == friendType) {
            tvAddToBlack.setVisibility(View.VISIBLE);
        } else if (AppKey.RELATIONSHIP_TYPE_BLACK == friendType) {
            tvRemoveBlack.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvReport:
                EventManager.ins().sendEvent(EventTag.DO_REPORT, 0, 0, null);
                dismiss();
                break;
            case R.id.tvAddToBlack:
                EventManager.ins().sendEvent(EventTag.DO_ADD_TO_BLACK_LIST, 0, 0, null);
                dismiss();
                break;
            case R.id.tvRemoveBlack:
                EventManager.ins().sendEvent(EventTag.DO_REMOVE_BLACK_LIST, 0, 0, null);
                dismiss();
                break;
            case R.id.tvCancelFollow:
                EventManager.ins().sendEvent(EventTag.DO_CANCEL_FOLLOW, 0, 0, null);
                dismiss();
                break;
            case R.id.tvCancel:
                dismiss();
                break;
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        backgroundAlpha(1f);
    }

    public void show(View parent) {
        if (!isShowing()) {
            this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            backgroundAlpha(0.5f);
        }
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

}

